﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CallBox.Entity;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Threading;

namespace CallBox.Dao
{
   public class clsMessageDAO
    {
              private static clsMessageDAO objIntancia;
        private static Mutex objMutex = new Mutex();
        public static clsMessageDAO Intancia
        {
            get
            {
                objMutex.WaitOne();
                if (objIntancia == null)
                {
                    objIntancia = new clsMessageDAO();
                }
                objMutex.ReleaseMutex();
                return objIntancia;
            }
        }
        public clsMessageDAO()
        {
        }

       private clsMessage GetEntity(DbDataReader Reader, clsMessage objMessage)
       {
           objMessage = new clsMessage();
           objMessage.Id = Reader["Id"] is DBNull ? 0 : Convert.ToInt32(Reader["Id"]);
           objMessage.Resources = new clsResources();
           objMessage.Resources.Path = Reader["Path"] is DBNull ? "" : Convert.ToString(Reader["Path"]);
           objMessage.Time = Reader["Time"] is DBNull ? "" : Convert.ToString(Reader["Time"]);
           
           objMessage.ReceiptDate = Reader["ReceiptDate"] is DBNull ? DateTime.Now : Convert.ToDateTime(Reader["ReceiptDate"]);
           objMessage.ReceiptDateString = Reader["ReceiptDate"] is DBNull ? DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") : Convert.ToDateTime(Reader["ReceiptDate"]).ToString("MM/dd/yyyy HH:mm:ss");

           objMessage.Mobile = new clsMobile();
           objMessage.Mobile.Number = Reader["Number"] is DBNull ? "" : Convert.ToString(Reader["Number"]);
           objMessage.Mobile.Number = objMessage.Mobile.Number.Trim();
           objMessage.Mobile.FirtsName = Reader["Name"] is DBNull ? "" : Convert.ToString(Reader["Name"]);
           
           return objMessage;
       }

       public List<clsMessage> GetbyContact(Int32 idContact)
       {
           SqlConnection db = null;
           List<clsMessage> Lista = new List<clsMessage>();
           DbDataReader Reader = null;
           clsMessage objCompany = null;
           try
           {
               db = clsConexion.ObtenerConecccion();
               SqlCommand ObjCmd = new SqlCommand("SP_GETMESSAGE", db);
               ObjCmd.Parameters.AddWithValue("@ID", idContact);
               ObjCmd.CommandType = CommandType.StoredProcedure;
               Reader = ObjCmd.ExecuteReader();

               if (Reader.HasRows)
               {
                   while (Reader.Read())
                   {
                       objCompany = GetEntity(Reader, objCompany);
                       Lista.Add(objCompany);
                   }
               }
               Reader.Close();
           }
           catch (Exception)
           {
               throw;
           }
           return Lista;
       }
       public Boolean Update(Int32 id, Int32 status)
       {
           SqlConnection db = null;
           Boolean Lista = false;
           DbDataReader Reader = null;
         
           try
           {
               db = clsConexion.ObtenerConecccion();
               SqlCommand ObjCmd = new SqlCommand("SP_SETPAPELERA", db);
               ObjCmd.Parameters.AddWithValue("@ID", id);
               ObjCmd.Parameters.AddWithValue("@STATUS", status);
               ObjCmd.CommandType = CommandType.StoredProcedure;
               ObjCmd.ExecuteNonQuery();

               Lista = true;
           }
           catch (Exception)
           {
               throw;
           }
           return Lista;
       }
       public List<clsMessage> GetbyPapelera()
       {
           SqlConnection db = null;
           List<clsMessage> Lista = new List<clsMessage>();
           DbDataReader Reader = null;
           clsMessage objCompany = null;
           try
           {
               db = clsConexion.ObtenerConecccion();
               SqlCommand ObjCmd = new SqlCommand("SP_GETPAPELERA", db);
               ObjCmd.CommandType = CommandType.StoredProcedure;
               Reader = ObjCmd.ExecuteReader();

               if (Reader.HasRows)
               {
                   while (Reader.Read())
                   {
                       objCompany = GetEntity(Reader, objCompany);
                       Lista.Add(objCompany);
                   }
               }
               Reader.Close();
           }
           catch (Exception)
           {
               throw;
           }
           return Lista;
       }
    }
}
